
#define M_data  7
#define M_clock 5

#define init_HIGH   0xffff      //11111111 11111111
#define init_LOW    0x0000      //00000000 00000000
#define init_LOW_HIGH    0x00ff      //00000000 11111111
#define init_HIGH_LOW    0xff00      //11111111 00000000
#define set_0 0x0800  //GAIN1
#define set_1 0x0400  //I0
#define set_2 0x0200  //I1
#define set_3 0x0100  //I2

#define set_4 0x1000  //GAIN0
#define set_5 0x2000  //ATT2
#define set_6 0x4000  //ATT1
#define set_7 0x8000  //ATT0

#define set_8 0x0008  //PRCALAC
#define set_9 0x0004  //SEL0
#define set_A 0x0002  //SEL1
#define set_B 0x0001  //NC D1571_Q7

#define set_C 0x0000  //REL1
#define set_D 0x0000  //REL1
#define set_E 0x0000  //REL0
#define set_F 0x0000  //REL0

/* NC       ////SET_BIT///-----0x**0*-First
 * first 
 * 
 * NC D1571_Q7// //00000000 00000001  0x0001
 * SEL1     // //00000000 00000010  0x0002
 * SEL0     // //00000000 00000100  0x0004
 * PRCALAC  // //00000000 00001000  0x0008
 * REL1     // //00000000 00000000  0x
 * REL1     // //00000000 00000000  0x
 * REL0     // //00000000 00000000  0x
 * REL0     // //00000000 00000000  0x
 * 
 * I2       // //00000001 00000000  0x0100
 * I1       // //00000010 00000000  0x0200
 * I0       // //00000100 00000000  0x0400
 * GAIN1    // //00001000 00000000  0x0800
 * GAIN0    // //00010000 00000000  0x1000
 * ATT2     // //00100000 00000000  0x2000
 * ATT1     // //01000000 00000000  0x4000
 * ATT0     // //10000000 00000000  0x8000
*/

void setup() {

  Init();              
}

void loop() {

/*  
 *   Serialout( init_LOW);        delay(500);                  

  Serialout( set_0);        delay(500);                  
    Serialout( set_1);        delay(500);                  
      Serialout( set_2);        delay(500);                  
        Serialout( set_3);        delay(500);                  
          Serialout( set_4);        delay(500);                  
            Serialout( set_5);        delay(500);                  
              Serialout( set_6);        delay(500);                  
                Serialout( set_7);        delay(500);                  

  Serialout( init_LOW);        delay(500);                  

                Serialout( set_7);        delay(500);                  
              Serialout( set_6);        delay(500);                  
            Serialout( set_5);        delay(500);                  
          Serialout( set_4);        delay(500);                  
        Serialout( set_3);        delay(500);                  
      Serialout( set_2);        delay(500);                  
    Serialout( set_1);        delay(500);                  
  Serialout( set_0);        delay(500);   */               

  Serialout( init_LOW);        delay(500);                  

  Serialout( set_0);        delay(500);                  
  Serialout( set_0|set_1);    delay(500);                  
  Serialout( set_0|set_1|set_2);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5|set_6);   delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5|set_6|set_7);   delay(500);                  
 
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5|set_6|set_7);   delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5|set_6);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4|set_5);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3|set_4);    delay(500);                  
  Serialout( set_0|set_1|set_2|set_3);    delay(500);                  
  Serialout( set_0|set_1|set_2);    delay(500);                  
  Serialout( set_0|set_1);   delay(500);                  
  Serialout( set_0);   delay(500);                  
 
}

void Init(){
  
  pinMode(M_clock, OUTPUT);
  pinMode(M_data, OUTPUT);
  digitalWrite(M_clock, HIGH);    
  delay(2);                  
  digitalWrite(M_clock, LOW);    
  delay(2);                  
  digitalWrite(M_clock, HIGH);    
  delay(2);      
 

}

void Serialout( int M_config){
  
  for(int i= 0; i < 16; i ++)
  {
    if((M_config&0x0001) == 0x0001) 
    {
      digitalWrite(M_data, LOW);      
  delayMicroseconds(20);
      if(i > 14){
        delayMicroseconds(20);
        digitalWrite(M_clock, LOW);       
        delay(1);                       
        digitalWrite(M_clock, HIGH);         
      }else{
        clk();
      }
    }else
    {
      digitalWrite(M_data, HIGH);      
  delayMicroseconds(20);
      if(i > 14){
        delayMicroseconds(20);
        digitalWrite(M_clock, LOW);       
        delay(1);                       
        digitalWrite(M_clock, HIGH);         
      }else{
        clk();
      }
    }
    M_config = M_config >> 1;
  }
}

void clk(){
  
  delayMicroseconds(20);
  digitalWrite(M_clock, LOW);    
  digitalWrite(M_clock, HIGH);   

}